package com.group4.rbac.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group4.rbac.entity.User;
import com.group4.rbac.mapper.UserMapper;
import com.group4.rbac.service.UserService;
import com.group4.rbac.util.Audience;
import com.group4.rbac.util.JwtUtil;
import com.study.exceptions.UserException;
import com.study.util.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author cloud
 * @since 2022-03-10
 */
@Service
@SuppressWarnings("all")
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private Audience audience;
    @Autowired
    private UserService userService;

    /**
     * 用户登陆
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult login(User user) {
        //数据校验
        if (user == null || user.getUsername() == null || user.getUsername().trim().length() == 0 || user.getPassword() == null || user.getPassword().trim().length() == 0) {
            return new ResponseResult<>(500, "请正确填写用户名和密码");
        }

            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            //账号查询
            queryWrapper.eq("username", user.getUsername());
            User u = userService.getOne(queryWrapper);
            if(u == null){
                return new ResponseResult(501,"账号不存在");
            }
            if(!user.getPassword().equals(u.getPassword())){
                return new ResponseResult(502,"密码错误");
            }
        String jwt = JwtUtil.createJwt(u.getId(), u.getUsername(), audience);
        int i = JwtUtil.parseJwtForId(jwt, audience);
        log.info("id:{}",i);
        return new ResponseResult<>(200, "登录成功", jwt);
    }


    /**
     * 用户注册
     *
     * @param user
     * @return
     */
    @Override
    public ResponseResult register(User user) {
        //数据有效性校验
        if (user == null) {
            return new ResponseResult(500, "请正确填写注册信息");
        }
        //账号校验
        if (user.getUsername() == null || user.getUsername().trim().length() == 0) {
            return new ResponseResult(500, "请填写用户名再注册");
        }
        //密码校验
        if (user.getPassword() == null || user.getPassword().trim().length() == 0) {
            return new ResponseResult(500, "请填写密码再注册");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        //根据用户名查重
        QueryWrapper<User> username = queryWrapper.eq("username", user.getUsername());
        User user1 = userMapper.selectOne(username);
        if (user1 != null) {
            return new ResponseResult(502, "账号已存在");
        }        //不重复进行用户注册

        //注册
        user.setGmtCreate(new Date());//设置注册时间
        int insert = userMapper.insert(user);
        if (insert == 0) {
            return new ResponseResult(501, "注册失败");
        }
        //结果返回
        return new ResponseResult(200, "注册成功");
    }


    /**
     * 用户列表
     *
     * @return
     */
    @Override
    public List<User> selectAll() {
        List<User> users = userMapper.selectAll();
        return users;
    }

    /**
     * 通过id查找账户
     *
     * @param id
     * @return
     */
    @Override
    public User getUserById(int id) {
        User user = userMapper.selectById(id);
        if (user == null) {
            throw new UserException("当前用户不存在");
        }
        return user;
    }

    @Override
    public ResponseResult<Integer> parseJWT(String jwt) {
        int i = JwtUtil.parseJwtForId(jwt, audience);
        if(i == 0){
            return new ResponseResult<>(500,"解析失败");
        }
        return new ResponseResult<>(200,"解析成功",i);
    }
}
